<template>
    <div class="app-container">
        <div>
            <p class="dialogTitle title">当前地震事件:
                <span v-if="currentEvent">{{ currentEvent.eventName }}</span>
                <span v-else>请在地震事件管理中设置当前地震事件</span>
            </p>
        </div>

        <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="140px" v-show="showSearch">
            <el-form-item label="审核状态" prop="status">
                <el-select v-model="queryParams.status" placeholder="请选择审核状态" clearable>
                    <el-option
                        v-for="item in [{ label: '未审核', value: '2' }, { label: '审核通过', value: '0' }, { label: '审核不通过', value: '1' }]"
                        :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="生命线类型" prop="lineType">
                <el-select v-model="queryParams.lineType" @change="chooseLineType" placeholder="请选择生命线类型" clearable>
                  <el-option v-for="item in option" :key="item.dictItemCode" :label="item.dictItemName"
                      :value="item.dictItemCode">
                  </el-option>
                </el-select>
                &nbsp;—&nbsp;
				<el-select v-model="queryParams.lineType2" placeholder="请选择" clearable>
					<el-option v-for="item in lineTypeArr2" :key="item.dictItemCode" :label="item.dictItemName" :value="item.dictItemCode">
					</el-option>
				</el-select>
            </el-form-item>
            <el-form-item label="破坏情况" prop="damage">
                <el-select v-model="queryParams.damage" placeholder="请选择破坏情况" clearable>
                    <el-option v-for="item in option1" :key="item.dictItemCode" :label="item.dictItemName" :value="item.dictItemCode">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="推测烈度" prop="intensity">
                <el-select v-model="queryParams.intensity" placeholder="请选择推测烈度" clearable>
                    <el-option v-for="item in option2" :key="item.dictItemCode" :label="item.dictItemName" :value="item.dictItemCode">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="生命线工程编号" prop="lifelineCode">
                <el-input v-model="queryParams.lifelineCode" placeholder="请输入生命线工程编号" clearable />
            </el-form-item>
            <!-- <el-form-item label="生命线工程名称" prop="lifelineName">
                <el-input v-model="queryParams.lifelineName" placeholder="请输入生命线工程名称" clearable />
            </el-form-item> -->
            <el-form-item label="调查员" prop="investigator">
                <el-input v-model="queryParams.investigator" placeholder="请输入调查员" clearable />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="handleQuery">搜索</el-button>
                <el-button @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" :disabled="!currentEvent || dataList.length == 0 || multiple"
                    @click="handleMoreAudie">批量审核</el-button>
            </el-col>
             <el-col :span="1.5">
                <el-button type="primary" :disabled="!currentEvent || dataList.length == 0"
                    @click="handleDel">批量删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="primary" :disabled="!currentEvent||dataList.length==0" @click="handleExport">导出</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="dataList" stripe  row-key="xlrId" @selection-change="handleSelectionChange">
            <el-table-column type="selection" align="center" :reserve-selection="true" width="55" />
            <el-table-column label="序号" align="center" width="55">
                <template #default="scope">
                    <span>{{ (queryParams.page - 1) * queryParams.rows + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
            <el-table-column label="生命线工程编号" align="center" prop="lifelineCode">
                <template #default="scope">
                    <span style="color: #107ce1;cursor: pointer;" @click="handleEdit(scope.row)">{{
                            scope.row.lifelineCode
                    }}</span>
                </template>
            </el-table-column>
            <el-table-column label="抽样点名称" align="center" prop="sampleName" />
            <el-table-column label="生命线工程名称" align="center" prop="lifelineName" />
            <el-table-column label="推测烈度" align="center" prop="intensity">
                <template #default="scope">
                    {{getIntensity(scope.row.intensity)}}
                </template>
            </el-table-column>
            <el-table-column label="生命线类型" align="center" prop="lineType">
                <el-table-column label="一级" align="center" prop="lineType" width="140">
					<template #default="scope">
				        {{getLineType(scope.row.lineType)}}
				    </template>
				</el-table-column>
				<el-table-column label="二级" align="center" prop="lineType2">
					<template #default="scope">
				        {{getLineType2(scope.row.lineType2)}}
				    </template>
				</el-table-column>
            </el-table-column>
            <el-table-column label="破坏情况" align="center" prop="damage">
                <template #default="scope">
                    {{getDamage(scope.row.damage)}}
                </template>
            </el-table-column>
            <el-table-column label="审核状态" align="center" prop="status">
                <template #default="scope">
                    <span v-if="scope.row.status == '2'">未审核</span>
                    <span v-if="scope.row.status == '0'">审核通过</span>
                    <span v-if="scope.row.status == '1'">审核不通过</span>
                </template>
            </el-table-column>
            <el-table-column label="调查员" align="center" prop="investigator" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button type="text" @click="handleAudit(scope.row)">审核</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page" v-model:limit="queryParams.rows"
            @pagination="getList" />


        <el-dialog title="查看生命工程数据" v-model="open" width="900px" append-to-body>
            <el-form ref="addRef" label-width="120px" :model="form" :rules="rules">
                <el-row style="margin: 20px 0;">
                    <el-col :span="24">
                        <el-form-item label="行政区划" required>
                            <span>{{ form.province }}{{ form.city }}{{ form.district }}{{ form.detailAddress }}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="抽样点名称" prop="sampleName">
                            <el-select v-model="form.sampleName" style="width: 100%;">
                                <el-option v-for="item in option4" :key="item.id" :label="item.samplingPointName"
                                    :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="经度" prop="longitude" required>
                            <el-input v-model="form.longitude" disabled/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="纬度" prop="latitude" required>
                            <el-input v-model="form.latitude" disabled/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="生命线工程名称" prop="lifelineName" required>
                            <el-input v-model="form.lifelineName" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="生命线类型" prop="lineType" required>
                            <el-select v-model="form.lineType" @change="chooseLineType2" placeholder="请选择生命线类型" clearable>
                              <el-option v-for="item in option" :key="item.dictItemCode" :label="item.dictItemName"
                                  :value="item.dictItemCode">
                              </el-option>
                            </el-select>
                            &nbsp;—&nbsp;
				            <el-select v-model="form.lineType2" placeholder="请选择" clearable>
				            	<el-option v-for="item in lineTypeArr" :key="item.dictItemCode" :label="item.dictItemName" :value="item.dictItemCode">
				            	</el-option>
				            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="破坏情况" prop="damage">
                            <el-select v-model="form.damage">
                                <el-option v-for="item in option1" :key="item.dictItemCode" :label="item.dictItemName" :value="item.dictItemCode">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="推测烈度" prop="intensity">
                            <el-select v-model="form.intensity" placeholder="请选择推测烈度" clearable>
                                <el-option v-for="item in option2" :key="item.dictItemCode" :label="item.dictItemName" :value="item.dictItemCode">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="具体描述情况">
                            <el-input v-model="form.detailDesc" type="textarea" maxlength="500" show-word-limit
                                :autosize="{ minRows: 5, maxRows: 5 }" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="照片">
                            <file-upload ref="uploadRef1" v-model="form.enclosurePhoto" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="视频">
                            <file-upload ref="uploadRef2" v-model="form.enclosureVideo" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="音频">
                            <file-upload ref="uploadRef3" v-model="form.enclosureAudio" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="相册">
                            <file-upload ref="uploadRef4" v-model="form.enclosureAlbum" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="handleUpdate">确 认</el-button>
                    <el-button @click="open = false">取 消</el-button>
                </div>
            </template>
        </el-dialog>

        <el-dialog title="审核管理" v-model="openAudit" width="600px" append-to-body>
            <el-form ref="auditRef" label-width="80px" :model="form1" :rules="rules">
                <el-row style="margin: 20px 0;">
                    <el-col>
                        <el-form-item label="审核结果" prop="status">
                            <el-select v-model="form1.status" style="width: 100%;">
                                <el-option v-for="item in [{ label: '通过', value: '0' }, { label: '不通过', value: '1' }]"
                                    :key="item.value" :label="item.label" :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="审核意见" prop="suggestion">
                            <el-input v-model="form1.suggestion" type="textarea" maxlength="500" show-word-limit
                                :autosize="{ minRows: 5, maxRows: 5 }" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="saveAudit">保 存</el-button>
                    <el-button @click="openAudit = false">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="lifelineEngineerAudit">
import useEventStore from "@/store/modules/event";
import { getAllList, getDetail, getLife, auditSave, getSamPoiList, updateAudit, deleteAudit } from "@/api/fieldWork/surveyAudit/lifelineEngineerAudit.js"
import { getDict } from "@/api/system/dict";
const { proxy } = getCurrentInstance();
const dataList = ref([]);
const ids = ref();
const loading = ref(false);
const single = ref(true);
const multiple = ref(true);
const open = ref(false);
const openAudit = ref(false);
const total = ref(0);
const showSearch = ref(true);
const option = ref([]);
const lineTypeArr2 = ref([]);
const lineTypeArr = ref([]);
const option1 = ref([]);
const lineTypeArr20 = ref([]);
const lineTypeArr21 = ref([]);
const lineTypeArr22 = ref([]);
const lineTypeArr23 = ref([]);
const lineTypeArr24 = ref([]);
const lineTypeArr25 = ref([]);
const lineTypeArr26 = ref([]);
const lineTypeArr27 = ref([]);
const lineTypeArr2All = ref([]);
const option2 = ref();
const currentEvent = computed(() => useEventStore().currentEvent);
const option4 = ref([]);
const selectstatus = ref([]);
const isMorestatus = ref(false);
const validatePositiveNumver = (rule, value, callback) => {
    if (/(^[1-9]\d*$)/.test(value)) {
        callback()
    } else if (!value) {
        callback()
    } else {
        callback(new Error('请输入正整数'))
    }
};
const validatePositiveNumver1 = (rule, value, callback) => {
    if (/(^[1-9]\d*$)/.test(value)) {
        callback()
    } else if (!value) {
        callback(new Error('建筑面积不能为空'))
    } else {
        callback(new Error('请输入正整数'))
    }
};
const data = reactive({
    queryParams: {
        page: 1,
        rows: 10,
        status: undefined,
        lineType: undefined,
        lineType2:undefined,
        damage: undefined,
        intensity: undefined,
        lifelineCode: undefined,
        lifelineName: undefined,
        investigator: undefined,
    },
    form: {},
    form1:{},
    rules:{
        name: [{ required: true, trigger: "blur", message: "房屋单体名称不能为空" }],
        structureType: [{ required: true, trigger: "change", message: "结构类型不能为空" }],
        destructionType: [{ required: true, trigger: "change", message: "破坏情况不能为空" }],
        floorage: [{required: false, validator: validatePositiveNumver1, trigger: "blur" }],
        lossValue: [{ required: false, validator: validatePositiveNumver, trigger: "blur" }],
        propertyLossProportion: [{ required: false, validator: validatePositiveNumver, trigger: "blur" }],
        status: [{ required: true, trigger: "change", message: "审核结果不能为空" }],
        lineType: [{ required: true, trigger: "change", message: "一级生命线类型不能为空" }],
        suggestion: [{ required: true, trigger: "blur", message: "审核意见不能为空" }],
    },
})
const { queryParams, form, form1, rules } = toRefs(data);

/** 多选框选中数据 */
function handleSelectionChange(selection) {
    single.value = selection.length != 1;
    multiple.value = !selection.length;
    ids.value = selection.map((item) => item.xlrId);
    selectstatus.value = selection;
};
// 批量
function handleMoreAudie() {
    isMorestatus.value = true;
    openAudit.value = true;
    proxy.resetForm("auditRef");
    selectstatus.value = selectstatus.value;
    // form1.value.ids = ids.value.join(',');
}
// 批量删除
function handleDel(){
    let str=''
    let arr=[]
    if(selectstatus.value.length!==0){
        selectstatus.value.forEach((v,i)=>{
            arr.push(v.xlrId)
        })
    }
    str=arr.toString('')
    proxy.$modal
      .confirm("请确认是否要删除选中该抽样点？",)
      .then(function () {
        deleteAudit({ids:str}).then(res=>{
            getList();
            proxy.$modal.msgSuccess("删除成功");
        })
      })
      .catch(() => { });
}

// 审核
function handleAudit(row) {
    isMorestatus.value = false;
    openAudit.value = true;
    proxy.resetForm("auditRef");
    // form1.value.ids = row.id;
    selectstatus.value = row;
}

function saveAudit() {
    proxy.$refs["auditRef"].validate(valid => {
        if (valid) {
            if(isMorestatus.value){
                selectstatus.value.forEach(item=>{
                    item.status = form1.value.status;
                    item.suggestion = form1.value.suggestion;
                    item.xsemId = currentEvent.value.id
                });
            }else{
                selectstatus.value = [{...selectstatus.value,status:form1.value.status,suggestion:form1.value.suggestion,xsemId: currentEvent.value.id}];
            }
            auditSave({list:selectstatus.value}).then(res => {
                proxy.$modal.msgSuccess("审核完成");
                openAudit.value = false;
                getList();
            })
        }
    })
}


/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.page = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    dataList.value = [];
    proxy.resetForm("queryRef");
    queryParams.value.lineType2 = undefined;
    handleQuery();
}

/** 导出按钮操作 */
function handleExport() {
    proxy.downloadTemplate('/lifeline/exportData', { ...queryParams.value,xsemId: currentEvent.value.id }, `生命工程数据.xls`)
}

// 编辑
function handleEdit(row) {
    detail(row.xlrId);
}

//处理附件格式
function eachFiles(fileName) {
    let deepFile = JSON.parse(JSON.stringify(proxy.$refs[fileName].fileList))
    let Ids = []
    for (var i = 0; i < deepFile.length; i++) {
        Ids.push(deepFile[i].attachId)
    }
    return Ids.join(",");
}

function handleUpdate() {
    form.value.enclosurePhoto = eachFiles('uploadRef1');
    form.value.enclosureVideo = eachFiles('uploadRef2');
    form.value.enclosureAudio = eachFiles('uploadRef3');
    form.value.enclosureAlbum = eachFiles('uploadRef4');
    proxy.$refs["addRef"].validate(valid => {
        if (valid) {
            updateAudit({ ...form.value, xsemId: currentEvent.value.id }).then(res => {
                proxy.$modal.msgSuccess("修改成功");
                open.value = false;
                getList();
            })
        }
    });
}

// 查看
function detail(id) {
    getDetail({ id: id }).then(res => {
        open.value = true;
        proxy.resetForm("addRef");
        form.value = res.data;
        const value = form.value.lineType;
        if(value=="1"){
		getDict("jtxt").then(response => {
	    lineTypeArr.value = response.data;
		});
	}else if(value=="2"){
		getDict("gsxt").then(response => {
        lineTypeArr.value = response.data;
		});
	}else if(value=="3"){
		getDict("syxt").then(response => {
        lineTypeArr.value = response.data;
		});
	}else if(value=="4"){
		getDict("rqxt").then(response => {
        lineTypeArr.value = response.data;
		});
	}else if(value=="5"){
		getDict("dlxt").then(response => {
        lineTypeArr.value = response.data;
		});
	}else if(value=="6"){
		getDict("txxt").then(response => {
        lineTypeArr.value = response.data;
		});
	}else if(value=="7"){
		getDict("slgc").then(response => {
        lineTypeArr.value = response.data;
		});
	}else {			
        lineTypeArr.value = [];
	}
    })
}

/** 查询数据列表 */
function getList() {
    if (currentEvent.value) {
        loading.value = true;
        getAllList({ ...queryParams.value, xsemId: currentEvent.value.id }).then(response => {
            dataList.value = response.data.rows ? response.data.rows : [];
            total.value = response.data.total;
            loading.value = false;
        });
    } else {
        dataList.value = [];
        total.value = 0;
    }
}

/** 查询所有抽样点名称 */
function getAllSamPoiList() {
    if (currentEvent.value) {
        getSamPoiList({ xsemId: currentEvent.value.id }).then(response => {
            option4.value = response.data;
        });
    } else {
        option4.value = [];
    }
}

function chooseLineType(value) {
		queryParams.value.lineType2 = "";
		if(value=="1"){
			getDict("jtxt").then(response => {
		    lineTypeArr2.value = response.data;
			});
		}else if(value=="2"){
			getDict("gsxt").then(response => {
		    lineTypeArr2.value = response.data;
			});
		}else if(value=="3"){
			getDict("syxt").then(response => {
		    lineTypeArr2.value = response.data;
			});
		}else if(value=="4"){
			getDict("rqxt").then(response => {
		    lineTypeArr2.value = response.data;
			});
		}else if(value=="5"){
			getDict("dlxt").then(response => {
		    lineTypeArr2.value = response.data;
			});
		}else if(value=="6"){
			getDict("txxt").then(response => {
		    lineTypeArr2.value = response.data;
			});
		}else if(value=="7"){
			getDict("slgc").then(response => {
		    lineTypeArr2.value = response.data;
			});
		}else {			
		  lineTypeArr2.value = [];
		}
		
	}

function chooseLineType2(value) {
	form.value.lineType2 = "";
	if(value=="1"){
		getDict("jtxt").then(response => {
	    lineTypeArr.value = response.data;
		});
	}else if(value=="2"){
		getDict("gsxt").then(response => {
        lineTypeArr.value = response.data;
		});
	}else if(value=="3"){
		getDict("syxt").then(response => {
        lineTypeArr.value = response.data;
		});
	}else if(value=="4"){
		getDict("rqxt").then(response => {
        lineTypeArr.value = response.data;
		});
	}else if(value=="5"){
		getDict("dlxt").then(response => {
        lineTypeArr.value = response.data;
		});
	}else if(value=="6"){
		getDict("txxt").then(response => {
        lineTypeArr.value = response.data;
		});
	}else if(value=="7"){
		getDict("slgc").then(response => {
        lineTypeArr.value = response.data;
		});
	}else {			
        lineTypeArr.value = [];
	}
		
	}

/** 获取字典值 */
async function getDictData() {
	await getDict("smxphqk").then(response => {
	    option1.value = response.data;
	});
	await getDict("tcld").then(response => {
	    option2.value = response.data;
	});
	await getDict("smxlx").then(response => { // 获取生命线类型
	    option.value = response.data;
	});
}
async function getDictData2(){
	await getDict("jtxt").then(response => {
    lineTypeArr21.value = response.data;
	});

	await getDict("gsxt").then(response => {
    lineTypeArr22.value = response.data;
	});
	await getDict("syxt").then(response => {
    lineTypeArr23.value = response.data;
	});
	await getDict("rqxt").then(response => {
    lineTypeArr24.value = response.data;
	});
	await getDict("dlxt").then(response => {
    lineTypeArr25.value = response.data;
	});
	await getDict("txxt").then(response => {
    lineTypeArr26.value = response.data;
	});
	await getDict("slgc").then(response => {
    lineTypeArr27.value = response.data;
	});
	lineTypeArr2All.value = [...lineTypeArr21.value,...lineTypeArr22.value
	,...lineTypeArr23.value,...lineTypeArr24.value,...lineTypeArr25.value,...lineTypeArr26.value,...lineTypeArr27.value]
//	console.log(structureTypeArr2All.value)
}
function getDamage(val){
  var name = ''
  option1.value.forEach(item => {
		if(item.dictItemCode == val) {
			name = item.dictItemName
			return
		}
	})
	return name
}
function getIntensity(val){
  var name = ''
  option2.value.forEach(item => {
		if(item.dictItemCode == val) {
			name = item.dictItemName
			return
		}
	})
	return name
}
function getLineType(val){
  var name = ''
  option.value.forEach(item => {
		if(item.dictItemCode == val) {
			name = item.dictItemName
			return
		}
	})
	return name
}
function getLineType2(val){
  var name = ''
  lineTypeArr2All.value.forEach(item => {
		if(item.dictItemCode == val) {
			name = item.dictItemName
			return
		}
	})
	return name
}


onMounted(() => {
    setTimeout(() => {
        getList();
        getAllSamPoiList();
    }, 200)
})
getDictData();
getDictData2();
</script>

<style scoped lang="scss">
.title {
    margin: 0 0 20px !important;
}
</style>

